package com.gkd.springboot_employeeadmin.controller;

import com.gkd.springboot_employeeadmin.pojo.Department;
import com.gkd.springboot_employeeadmin.pojo.Employee;
import com.gkd.springboot_employeeadmin.service.DepartmentService;
import com.gkd.springboot_employeeadmin.service.EmployeeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.stereotype.Repository;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import javax.jws.WebParam;
import javax.websocket.server.PathParam;
import java.util.List;

@Controller
public class EmployeeController {
    @Autowired
    private EmployeeService employeeService;
    @Autowired
    private DepartmentService departmentService;

//    /**
//     * 获取所有员工，并返回到显示页
//     * @param model
//     * @return
//     */
//    @RequestMapping("/emps")
//    public String allemp(Model model){
//        List<Employee> allemp = employeeService.allemp();
//        model.addAttribute("emps",allemp);
//        return "emp/list";
//    }

//    private int indexpage = (currPage-1) * pageSize;  //开始索引
    @RequestMapping("/emps")
    public String allemp(Model model,String currPage)
    {
        String cu=currPage;
        int pageno = 0 ;//页数
        int pageSize = 5;//每页大小

        if(cu==null)
        {
            pageno=1;
        }else {
            Integer integer = Integer.valueOf(cu);
            if(integer<1){
                pageno=1;
            }else {
            pageno=integer;
        }}
        int sumemp = employeeService.sumemp();
        int maxpage=sumemp%pageSize==0?sumemp/pageSize:sumemp/pageSize+1;
        if(maxpage<pageno)
        {
            pageno=maxpage;
        }
        int indexpage = (pageno - 1) * pageSize;
        List<Employee> employees =employeeService.pagebean(indexpage,pageSize);

        model.addAttribute("emps",employees);
        model.addAttribute("pageno",pageno);
        model.addAttribute("maxpage",maxpage);
        return "emp/list";
    }


    /**
     *把员工部分信息带到添加页面上
     * @param model
     * @return
     */
    @GetMapping("/emp")
    public String listall(Model model)
    {
        List<Department> departments = departmentService.listall();
        model.addAttribute("departments",departments);
        return "emp/add";
    }
    /**
     * 添加消息
     * @param employee
     * @return
     */
    @PostMapping("/emp")
    public String addEmploy(Employee employee)
    {
        Integer id = employee.getDepartment().getId();
        employee.setDid(id);
        employeeService.addEmploy(employee);
        return "redirect:/emps";
    }

    /**
     * 把数据带去员工的修改页面
     */
    @GetMapping("/emp/{id}")
    public String toUpdateEmp(@PathVariable("id") Integer id, Model model){
        //员工信息
        Employee employee = employeeService.queryEmpById(id);
        model.addAttribute("emp",employee);
        //获取全部的部门消息
        List<Department> listall = departmentService.listall();
        model.addAttribute("departments",listall);
        return "emp/update";
    }

    /**
     * 修改员工
     * @return
     */
    @PostMapping("/updateEmp")
    public String updateEmp(Employee employee)
    {
        Integer id = employee.getDepartment().getId();
        employee.setDid(id);
        employeeService.updateEmp(employee);
        return "redirect:/emps";
    }

    /**删除员工
     * @param id
     * @return
     */
    @RequestMapping("/deleteemp/{id}")
    public String deleteEmpById(@PathVariable("id") Integer id){
        employeeService.deleteEmpById(id);
        return "redirect:/emps";

    }
}












